package com.xfcycle.zebrabikegp;

import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import java.util.UUID;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    /**
     * 测试从配置文件取uuid
     *
     * @throws Exception
     */
    @Test
    public void useAppContext() throws Exception {

        InputStream is = getClass().getClassLoader().getResourceAsStream("assets/config.properties");
        Properties props = new Properties();
        props.load(is);
        String[] param = props.getProperty("device.uuids").split(":");
        UUID[] uuids = new UUID[param.length];
        for (int i = 0; i < param.length; i++) {
            UUID uuid = UUID.fromString(param[i]);
            uuids[i] = uuid;
        }

        for (UUID u : uuids
                ) {
            System.out.println(u);
        }

        is.close();
    }

    @Test
    public void getDataFromWeb() throws Exception {

        String result = "";
        InputStream is = null;
        BufferedReader br = null;
        StringBuffer data_cache = new StringBuffer();

        try {

            String param = "uid=1";
            String requestPath = "http://10.0.1.165:8080/zigbeServer/userLogin";

            //获取get请求 计算Authorization时需要的请求地址
            StringBuffer path = new StringBuffer().append(requestPath);
            if (null != param) {
                path = path.append("?").append(param);
            }


            URL realUrl = new URL(path.toString());
            //获取和Url之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

            Log.e("----", "getDataFromWeb: -------------------------------------- "+path);

            //设置请求的通用属性
//            if (!path.contains("public")) {
//                //判断该请求是否是public请求，不是，则需要验证请求的合法性
//                connection.setRequestProperty("Authorization", getAuthorization("GET\n"+path));
//            }
//
//            //验证请求是否是同一个设备发出的,传入值为设备号
//            GetDevicesUUIDUtil getDevicesUUIDUtil = GetDevicesUUIDUtil.getInstance(context);
//            connection.setRequestProperty("User-Agent", getDevicesUUIDUtil.getDevicesUUIDByMD5());

            //建立实际的连接
            connection.connect();

            //判断返回码获得相应的is流
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_CREATED) {
                is = connection.getInputStream();
            } else {
                is = connection.getErrorStream();
            }
            br = new BufferedReader(new InputStreamReader(is));

            //接收返回的结果
            String line = "";
            while (null != (line = br.readLine())) {
                data_cache.append(line);
            }

            result = data_cache.toString();

            //关闭网络连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
            result = "defeat";
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
                if (null != br) {
                    br.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

        Log.e("----", "getDataFromWeb: ------------ "+result);
    }
}
